package Study.Work;

import java.util.Arrays;

public class CreatQueue extends Queue{
    Object[] arr;
    int count;
    int capacity = 4 ;
    public CreatQueue(){
        arr = new Object[capacity];
    }
    public CreatQueue(int capacity){
        if (capacity <0 ){
            capacity = 10;
        }
        this.capacity = capacity;
        arr = new Object[capacity];
    }

    @Override
    void enqueue(Object element) {
        grow();
        arr[count] = element;
        count ++;
    }

    @Override
    Object dequeue() {
       if (arr[0] == null){
           return  null;
       }
       Object remove = arr[0];
       System.arraycopy(arr, 1, arr, 0, arr.length - 1);
       count --;
       return remove;
    }

    @Override
    Object peek() {
        if (count  ==  0) {
            return null;
        }else {
            return arr[0];
        }
    }

    @Override
    boolean isEmpty() {
       return count == 0;
    }

    @Override
    int size() {
        return count;
    }

    @Override
    void grow() {
        if (count >= arr.length) {
            Object[] newArr = new Object[capacity * 2];
            System.arraycopy(arr, 0, newArr, 0, arr.length);
            arr = newArr;
        }
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("CreatQueue = {");
        for (int i = 0; i < count ; i++) {
            sb.append(arr[i] + ",");
        }
        sb.append("}");
    return sb.toString();
    }
}
